package com.pratices.rocketMQ;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

import java.nio.charset.Charset;

public class ProduceDemo {
    public static void main(String[] args) throws MQClientException {
        //实例生产者
        DefaultMQProducer myProducerGroup = new DefaultMQProducer("myProducerGroup");
        //指定Nameserver地址
        myProducerGroup.setNamesrvAddr("192.168.32.100:9876");
        //启动生产者
        myProducerGroup.start();
        try {
            //创建消息
            Message message = new Message("myTopic", "myTag", "hello world".getBytes(Charset.forName("utf-8")));
            //发送消息到Broker
            SendResult sendResult = myProducerGroup.send(message);
            System.out.printf("%s%n",sendResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
        myProducerGroup.shutdown();
    }
}
